
using Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace SOEI.Solution.DomainEntities.ContractManagement.DomainService
{
    /// <summary>
    /// 领域服务层一个模块的核心业务逻辑
    ///</summary>
    public class ContractBodyManager : DomainServiceBase, IContractBodyManager
    {

        private readonly IRepository<ContractBody, string> _contractbodyRepository;

        /// <summary>
        /// 【ContractBody】的构造方法
        /// 通过构造函数注册服务到依赖注入容器中
        ///</summary>
        public ContractBodyManager(IRepository<ContractBody, string> contractbodyRepository)
        {
            _contractbodyRepository = contractbodyRepository;
        }

        #region -------------------------------------------------辅助工具生成---------------------------------------------- 

        /// <summary>
        /// 返回列表查询用
        /// </summary>
        /// <returns></returns>
        public IQueryable<ContractBody> QueryEntityListAsNoTracking()
        {

            var query = _contractbodyRepository.GetAll().AsNoTracking()
                        .Select(x => x);
            return query;
        }

        /// <summary>
        /// 【ContractBody】返回表达式数的实体信息即IQueryable类型
        /// </summary>
        /// <returns>IQueryable</returns>
        public IQueryable<ContractBody> Query()
        {
            return _contractbodyRepository.GetAll();
        }

        /// <summary>
        /// 【ContractBody】返回即IQueryable类型的实体，不包含EF Core跟踪标记
        /// </summary>
        /// <returns>IQueryable</returns>
        public IQueryable<ContractBody> QueryAsNoTracking()
        {
            return _contractbodyRepository.GetAll().AsNoTracking();
        }

        /// <summary>
        /// 【ContractBody】创建实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ContractBody> CreateAsync(ContractBody entity)
        {
            await _contractbodyRepository.InsertAndGetIdAsync(entity);
            return entity;
        }

        #endregion


        #region -------------------------------------------------用户自定义------------------------------------------------
        /*请在此扩展领域服务接口*/

        /// <summary>
        /// 【ContractBody】根据合同Id查询实体信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns>实体</returns>
        public async Task<List<ContractBody>> FindByContractSummaryIdAsync(string contractSummaryId)
        {
            return await _contractbodyRepository
                .GetAll()
                .Where(zw => string.Equals(zw.ContractSummaryId, contractSummaryId, StringComparison.OrdinalIgnoreCase))
                .Select(zw => new ContractBody()
                {
                    ContractSummaryId = zw.ContractSummaryId,
                    Key = zw.Key,
                    Value = zw.Value
                })
                .ToListAsync();
        }

        /// <summary>
        /// 【ContractBody】批量删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task BatchDeleteByContractSummaryId(List<string> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _contractbodyRepository.DeleteAsync(a => input.Contains(a.ContractSummaryId));
        }

        #endregion









    }
}
